package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.system.dao.FloorMapper;
import com.qinglei.recoup.system.domain.Floor;
import com.qinglei.recoup.system.pojo.FloorPageParam;
import com.qinglei.recoup.system.pojo.FloorShowVO;
import com.qinglei.recoup.system.service.FloorService;
import com.qinglei.recoup.system.service.MonitorTimeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author syp
 */
@Service
@AllArgsConstructor
public class FloorServiceImpl extends ServiceImpl<FloorMapper, Floor> implements FloorService {

    private FloorMapper floorMapper;

    @Override
    public CommonRes<IPage<FloorShowVO>> showPageFloor(FloorPageParam floorPageParam) {
        Page<FloorShowVO> page = new Page<>(floorPageParam.getPageNum(), floorPageParam.getPageSize());
        IPage<FloorShowVO> buildingShowVOIPage = floorMapper.showPageFloor(page, floorPageParam.getBuildingId());
        return CommonRes.ok(buildingShowVOIPage);
    }

    @Override
    public CommonRes addBedCnt(Integer id) {
        Floor floor = getBaseMapper().selectById(id);
        floor.setBedCnt(floor.getBedCnt() + 1);
        int count = getBaseMapper().updateById(floor);
        if (count > 0) {
            return CommonRes.ok();
        }
        throw new RuntimeException("添加层床位数量失败");
    }

    @Override
    public CommonRes subBedCnt(Integer id, Integer cnt) {
        Floor floor = getBaseMapper().selectById(id);
        if (floor.getBedCnt() > cnt) {
            floor.setBedCnt(floor.getBedCnt() - cnt);
        } else {
            floor.setBedCnt(0);
        }
        int count = getBaseMapper().updateById(floor);
        if (count > 0) {
            return CommonRes.ok();
        }
        throw new RuntimeException("减少层床位数量失败");
    }

    /**
     * 修改监测时间是否统一
     *
     * @param id
     * @return
     */
    @Override
    public Boolean updateMonitorTimeUnified(Integer id) {
        try {
            Floor floor = new Floor();
            floor.setId(id);
            floor.setMonitorTimeUnified(MonitorTimeService.UNIFIED_YES);
            baseMapper.updateById(floor);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return true;
    }

    /**
     * 根据楼id查询层
     *
     * @param buildingId
     * @return
     */
    @Override
    public CommonRes<List<Floor>> getFloorByBuildingId(Integer buildingId) {
        return CommonRes.ok(baseMapper.selectList(Wrappers.<Floor>lambdaUpdate().eq(Floor::getBuildingId, buildingId)));
    }

    /**
     * 修改离床阀值时间统一
     *
     * @param objId
     * @param outBedTimeout
     * @return
     */
    @Override
    public boolean updateOutBedTime(Integer objId, String outBedTimeout) {
        Floor floor = new Floor();
        floor.setId(objId);
        floor.setOutBedTimeout(outBedTimeout);
        floor.setOutBedTimeoutUnified(MonitorTimeService.UNIFIED_YES);
        int count = baseMapper.updateById(floor);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改离床阀值时间不统一
     *
     * @param buildingId
     * @return
     */
    @Override
    public boolean updateOutBedTimeNo(Integer buildingId) {
        Floor floor = new Floor();
        floor.setId(buildingId);
        floor.setOutBedTimeoutUnified(MonitorTimeService.UNIFIED_NO);
        int count = baseMapper.updateById(floor);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改监控时间不统一
     *
     * @param buildingId
     * @return
     */
    @Override
    public boolean updateMonitorTimeNo(Integer buildingId) {
        Floor floor = new Floor();
        floor.setId(buildingId);
        floor.setMonitorTimeUnified(MonitorTimeService.UNIFIED_NO);
        int count = baseMapper.updateById(floor);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public String getNameWithParent(Integer floorId) {
        String name = baseMapper.selectNameWithParent(floorId);
        if (name == null) {
            name = "";
        }
        return name;
    }
}
